<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>事件</title>
    <style type="text/css">
        #container{
            width: 100px;
            height: 80px;
            border: 1px solid rgb(255,0,0);
        }
    </style>
</head>
<body>

<!--<div id="container" onmouseover="changeBackgroundColor()">-->
<div id="container">
</div>
<button type="button"  onclick="removeDivListener()">解除事件监听</button>
<script type="text/javascript">

    let myfunction = function(){
        let r = Math.random() * 255 ;
        let g = Math.random() * 255 ;
        let b = Math.random() * 255 ;
        div.style.backgroundColor = "rgb(" + r + " , " + g + "," + b +")" ;
    }


    function changeBackgroundColor() {
        // 选中对应的元素
        let div = document.querySelector("#container") ;

        myfunction()
    }
    let div = document.querySelector("#container") ;

    // div.addEventListener( 事件名称 , 具体的执行动作 ) ;
    div.addEventListener( "mouseover" , myfunction) ;

   function removeDivListener() {
       div.removeEventListener( "mouseover" , myfunction) ;
   }
</script>


</body>
</html>